package lumis.report;

import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtNewMethod;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import lumis.service.report.ShowReportControllerHtml;

/**
 * Initialization class of portal's jasper reports.
 * 
 * @version $Revision: 11180 $ $Date: 2009-12-04 18:13:02 -0200 (Fri, 04 Dec 2009) $
 * @since 5.5.0
 */
public class ReportServletContextListener implements ServletContextListener
{
	private static final String SET_CELL_VALUE_METHOD = "public void setCellValue(org.apache.poi.hssf.usermodel.HSSFRichTextString p){setCellValue((org.apache.poi.ss.usermodel.RichTextString) p);}";
	private static final String ADD_MERGED_REGION_METHOD = "public int addMergedRegion(org.apache.poi.hssf.util.CellRangeAddress p){return addMergedRegion((org.apache.poi.ss.util.CellRangeAddress) p);}";

	public void contextDestroyed(ServletContextEvent event)
	{

	}

	public void contextInitialized(ServletContextEvent event)
	{
		try
		{
			ClassPool classPool = new ClassPool();
			classPool.insertClassPath(new ClassClassPath(ShowReportControllerHtml.class));

			CtClass ctClass = classPool.get("org.apache.poi.hssf.usermodel.HSSFSheet");
			ctClass.addMethod(CtNewMethod.make(ADD_MERGED_REGION_METHOD, ctClass));
			ctClass.toClass();

			ctClass = classPool.get("org.apache.poi.hssf.usermodel.HSSFCell");
			ctClass.addMethod(CtNewMethod.make(SET_CELL_VALUE_METHOD, ctClass));
			ctClass.toClass();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	}
}
